# 导入序列化器
from .serializers import *

# 创建日志对象
import logging

logger = logging.getLogger("member")
# Create your views here.


class MemberLevelruleListViewSet(viewsets.ModelViewSet):
    __title__ = "会员等级规则"
    __group__ = "会员模块"
    __info__ = {
        "get": "查询会员等级规则列表",
        "post": "创建会员等级规则",
        "put": "编辑会员等级规则",
        "delete": "移除会员等级规则",
    }
    __doc__ = """	list:
    <h4>查询会员等级规则列表</h4>
    retrieve:
    <h4>检索会员等级规则详情</h4>
    create:
    <h4>创建会员等级规则</h4>
    update:
    <h4>编辑会员等级规则</h4>
    delete:
    <h4>移除会员等级规则</h4>"""
    http_method_names = ["get", "post", "put", "delete"]
    # authentication_classes = []
    # permission_classes = [AllPermissions]
    authentication_classes = [
        MyJWTAuthentication,
        WeChatJWTAuthentication,
        BarJWTAuthentication,
        XboxJWTAuthentication,
    ]
    permission_classes = [
        MyPermissions | WeChatPermissions | BarPermissions | XboxPermissions
    ]
    serializer_class = MemberLevelruleListSerializer
    queryset = MemberLevelruleList.objects.all()

    filter_backends = [ListDjangoFilterBackend]
    filterset_fields = ["status", "pid", "master"]

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        serializer = self.get_serializer(queryset, many=True)
        return Response({"rescode": 200, "resmsg": serializer.data})

    def create(self, request, *args, **kwargs):
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        if serializer.data["status"] == 1:
            raise serializers.ValidationError({"status": "禁止创建默认等级"})
        auth = request.user.id or 0
        serializer.save(master=auth, curtime=timezone.now())
        return Response({"rescode": 200, "resmsg": serializer.data})

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        return Response({"rescode": 200, "resmsg": serializer.data})

    def update(self, request, *args, **kwargs):
        partial = True
        instance = self.get_object()
        if instance.status == 1:
            raise serializers.ValidationError({"status": "禁止编辑默认等级"})
        serializer = self.get_serializer(instance, data=request.data, partial=partial)
        serializer.is_valid(raise_exception=True)
        if serializer.data["status"] == 1:
            raise serializers.ValidationError({"status": "禁止编辑默认等级"})
        auth = request.user.id or 0
        serializer.save(master=auth, curtime=timezone.now())
        return Response({"rescode": 200, "resmsg": serializer.data})

    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        if instance.status == 1:
            raise serializers.ValidationError({"status": "禁止移除默认等级"})
        self.perform_destroy(instance)
        return Response({"rescode": 200, "resmsg": "SUCCESS"})


class MemberLevelCountViewSet(viewsets.ModelViewSet):
    __title__ = "按等级规则统计会员"
    __group__ = "会员模块"
    __info__ = {"get": "查询", "post": "创建", "put": "编辑", "delete": "移除"}
    __doc__ = """	
    list:
    <h4>按等级规则统计会员(禁用)</h4>    
    retrieve:
    <h4>按等级规则统计会员</h4>
        /{会员等级ID}/
    """
    http_method_names = ["get"]
    # authentication_classes = []
    # permission_classes = [AllPermissions]
    authentication_classes = [
        MyJWTAuthentication,
        BoxJWTAuthentication,
        WeChatJWTAuthentication,
        XboxJWTAuthentication,
    ]
    permission_classes = [
        MyPermissions | BoxPermissions | WeChatPermissions | XboxPermissions
    ]
    serializer_class = NoneSerializer
    queryset = None

    def list(self, request, *args, **kwargs):
        # queryset = self.filter_queryset(self.get_queryset())
        # serializer = self.get_serializer(queryset, many=True)
        return Response({"rescode": 200, "resmsg": "该接口禁用"})

    def retrieve(self, request, *args, **kwargs):
        # instance = self.get_object()
        # serializer = self.get_serializer(instance)
        pk = kwargs.get("pk")

        rule = (
            MemberLevelruleList.objects.filter(id=pk)
            .values("lastreg", "week", "lol", "csgo", "steam", "other", "onpay", "pid")
            .first()
        )
        # print(rule)
        from .tasks import MemberLevelCountTasks

        # r = MemberLevelCountTasks(rule)
        r = MemberLevelCountTasks.apply_async((rule,)).task_id
        return Response({"rescode": 200, "resmsg": r})


# 检查视图
filter_views(globals())
